"use strict";

/**
 * @import { Compiler, RspackPluginInstance } from "@rspack/core";
 */

/**
 * @implements {RspackPluginInstance}
 */
class GlobalFunctionPlugin {
  static Name = "GlobalFunctionPlugin";

  /**
   * @param {Compiler} compiler
   * @returns {void}
   */
  apply(compiler) {
    compiler.compile;
    compiler.hooks.compilation.tap(GlobalFunctionPlugin.Name, (compilation) => {
      // compiler.hooks
      // NOTE: Not have this hooks in rspack
      // compilation.hooks.optimizeChunkAssets.tap(
      //     "GlobalFunctionPlugin",
      //     (chunks) => {
      //       chunks.forEach((chunk) => {
      //         chunk.files.forEach((file) => {
      //           const source = compilation.assets[file].source();
      //           const newSource = `function myGlobalFunction() { /* 函数实现 */ }\n${source}`;
      //           compilation.assets[file] = {
      //             source: () => newSource,
      //             size: () => newSource.length,
      //           };
      //         });
      //       });
      //     },
      // );
    });
  }
}

module.exports = { GlobalFunctionPlugin };
